package org.anarcoding.twitterbot.services;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

import org.anarcoding.twitterbot.model.AnarcoTwitterException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class IAnarcoTwitterSystemServicesImplBuilder {

	private static final String	IBATIS_CONFIGURACION	= "org/anarcoding/twitterbot/db/configuracion_db.xml";
	private static final String	DRIVER_DB				= "org.sqlite.JDBC";
	private static final String	URL_DB					= "jdbc:sqlite:" + "." + File.separator + "anarco_twitter_db.sqlite";

	private IAnarcoTwitterSystemServicesImplBuilder() {
	}

	public static IAnarcoTwitterSystemServices build() throws AnarcoTwitterException {
		Reader reader;
		try {
			reader = Resources.getResourceAsReader( IBATIS_CONFIGURACION );
			Properties datosDeConexion = new Properties();

			datosDeConexion.setProperty( "driver", DRIVER_DB );
			datosDeConexion.setProperty( "url", URL_DB );

			SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build( reader, datosDeConexion );

			IAnarcoTwitterSystemServices services = new IAnarcoTwitterSystemServicesImpl( sessionFactory );

			return services;
		} catch( IOException e ) {
			e.printStackTrace();
			throw new AnarcoTwitterException( e, "Error en la construccion de los servicios del sistema.\r\n" );
		}
	}
}